package bb.lanxing.lib.devices.core.utils;

import android.bluetooth.BluetoothGattCharacteristic;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.ParcelUuid;
import android.os.Parcelable;
import java.util.UUID;

public final class CharacteristicValueHelper {
    public static final String ACTION_CHARACTERISTIC_VALUE = "action_characteristic_value";
    public static final String ACTION_READ_CHARACTERISTIC = "action_read_characteristic";
    public static final String EXTRA_ADDRESS = "extra_address";
    public static final String EXTRA_CHARACTERISTIC = "extra_characteristic";
    public static final String EXTRA_UUID = "extra_uuid";
    public static final String EXTRA_VALUE = "extra_value";
    private BroadcastReceiver characteristicValueReceiver;
    private Context context;
    private OnCharacteristicValueChangedListener onCharacteristicValueChangedListener;

    public interface OnCharacteristicValueChangedListener {
        void onCharacteristicValue(CharacteristicValueHelper characteristicValueHelper, String str, BluetoothGattCharacteristic bluetoothGattCharacteristic);
    }

    public CharacteristicValueHelper(Context context, OnCharacteristicValueChangedListener onCharacteristicValueChangedListener) {
        this.context = context;
        this.onCharacteristicValueChangedListener = onCharacteristicValueChangedListener;
        register();
    }

    private void register() {
        if (this.characteristicValueReceiver != null) {
            return;
        }
        this.characteristicValueReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                if (CharacteristicValueHelper.ACTION_CHARACTERISTIC_VALUE.equals(intent.getAction())) {
                    CharacteristicValueHelper.this.dispatchCharacteristicValue(intent);
                }
            }
        };
        this.context.registerReceiver(this.characteristicValueReceiver, new IntentFilter(ACTION_CHARACTERISTIC_VALUE));
    }

    private void unregister() {
        BroadcastReceiver broadcastReceiver = this.characteristicValueReceiver;
        if (broadcastReceiver == null) {
            return;
        }
        this.context.unregisterReceiver(broadcastReceiver);
        this.characteristicValueReceiver = null;
    }

    public void dispatchCharacteristicValue(Intent intent) {
        if (this.onCharacteristicValueChangedListener != null) {
            BluetoothGattCharacteristic bluetoothGattCharacteristic = (BluetoothGattCharacteristic) intent.getParcelableExtra(EXTRA_CHARACTERISTIC);
            String stringExtra = intent.getStringExtra(EXTRA_ADDRESS);
            if (bluetoothGattCharacteristic == null) {
                bluetoothGattCharacteristic = new BluetoothGattCharacteristic(((ParcelUuid) intent.getParcelableExtra(EXTRA_UUID)).getUuid(), 0, 0);
            }
            bluetoothGattCharacteristic.setValue(intent.getByteArrayExtra(EXTRA_VALUE));
            this.onCharacteristicValueChangedListener.onCharacteristicValue(this, stringExtra, bluetoothGattCharacteristic);
        }
    }

    public void release() {
        unregister();
        this.context = null;
    }

    public static void notifyCharacteristicValue(Context context, String str, BluetoothGattCharacteristic bluetoothGattCharacteristic) {
        if (bluetoothGattCharacteristic == null) {
            return;
        }
        Intent intent = new Intent(ACTION_CHARACTERISTIC_VALUE);
        if (bluetoothGattCharacteristic instanceof Parcelable) {
            intent.putExtra(EXTRA_CHARACTERISTIC, bluetoothGattCharacteristic);
        }
        intent.putExtra(EXTRA_UUID, new ParcelUuid(bluetoothGattCharacteristic.getUuid()));
        intent.putExtra(EXTRA_VALUE, bluetoothGattCharacteristic.getValue());
        intent.putExtra(EXTRA_ADDRESS, str);
        intent.setPackage(context.getPackageName());
        context.sendBroadcast(intent);
    }

    public static void readCharacteristicValue(Context context, String str, UUID uuid) {
        Intent intent = new Intent(ACTION_READ_CHARACTERISTIC);
        intent.putExtra(EXTRA_UUID, new ParcelUuid(uuid));
        intent.putExtra(EXTRA_ADDRESS, str);
        intent.setPackage(context.getPackageName());
        context.sendBroadcast(intent);
    }
}
